package io.kubesphere.devops.core;

import io.kubesphere.devops.model.UserMergingRule;

import java.util.ArrayList;
import java.util.List;

public class DynamicDataSourceContextHolder {

    private static final ThreadLocal<String> CONTEXT_HOLDER = ThreadLocal.withInitial(PartitionZone.ORGIN_MASTER::name);

    private static List<Object> dataSourceKeys = new ArrayList<>();

    public static void setDataSourceKey(String key) {
        CONTEXT_HOLDER.set(key);
    }

    public static String getDataSourceKey() {
        return CONTEXT_HOLDER.get();
    }

    public static void clearDataSourceKey() {
        CONTEXT_HOLDER.remove();
    }

    public static List<Object> getDataSourceKeys() {
        return dataSourceKeys;
    }

    public static void routePartition(PartitionZone partitionZone, UserMergingRule rule){
        CONTEXT_HOLDER.set(partitionZone.name()+rule.getSchemaName());
    }

    public static void routeMaster(){
        CONTEXT_HOLDER.set(PartitionZone.ORGIN_MASTER.name());
    }
}
